//
//  SLMP4Object.m
//  SLVideoParser
//
//  Created by S_LINE on 2020/6/18.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLMP4Box.h"

@implementation SLMP4Box

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupData];
    }
    return self;
}

- (void)setupData
{
    self.header = [[SLMP4BoxHeader alloc] init];
}

- (void)printBox
{
    printf("offset = %llu", self.offset);
    printf("header.size = %llu", self.header.size);
    printf("header.type = %s", [self.header.typeDesc UTF8String]);
    
    if (![self.boxes isKindOfClass:[NSArray class]])
    {
        return;
    }
    
    printf("{");
    for (int i = 0; i < self.boxes.count; i++) {
        SLMP4Box *box = [self.boxes objectAtIndex:i];
        [box printBox];
    }
    printf("}");
}

@end

@implementation SLMP4BoxHeader

- (NSString *)typeDesc
{
    switch (self.type) {
        case SLMP4BoxType_none:
            return @"none";
            break;
        case SLMP4BoxType_ftyp:
            return @"ftyp";
            break;
        case SLMP4BoxType_free:
            return @"free";
            break;
        case SLMP4BoxType_mdat:
            return @"mdat";
            break;
        case SLMP4BoxType_moov:
            return @"moov";
            break;
        // moov or trak sub
        case SLMP4BoxType_mvhd:
            return @"mvhd";
            break;
        case SLMP4BoxType_trak:
            return @"trak";
            break;
        case SLMP4BoxType_udta:
            return @"udta";
            break;
        // trak sub
        case SLMP4BoxType_tkhd:
            return @"tkhd";
            break;
        case SLMP4BoxType_edts:
            return @"edts";
            break;
        case SLMP4BoxType_mdia:
            return @"mdia";
            break;
        // mdia sub
        case SLMP4BoxType_mdhd:
            return @"mdhd";
            break;
        case SLMP4BoxType_hdlr:
            return @"hdlr";
            break;
        case SLMP4BoxType_minf:
            return @"minf";
            break;
        // minf sub
        case SLMP4BoxType_vmhd:
            return @"vmhd";
            break;
        case SLMP4BoxType_smhd:
            return @"smhd";
            break;
        case SLMP4BoxType_dinf:
            return @"dinf";
            break;
        case SLMP4BoxType_stbl:
            return @"stbl";
            break;
        // dinf sub
        case SLMP4BoxType_dref:
            return @"dref";
            break;
        // stbl sub
        case SLMP4BoxType_stsd:
            return @"stsd";
            break;
        case SLMP4BoxType_stts:
            return @"stts";
            break;
        case SLMP4BoxType_stss:
            return @"stss";
            break;
        case SLMP4BoxType_ctts:
            return @"ctts";
            break;
        case SLMP4BoxType_stsc:
            return @"stsc";
            break;
        case SLMP4BoxType_stsz:
            return @"stsz";
            break;
        case SLMP4BoxType_stco:
            return @"stco";
            break;
        case SLMP4BoxType_sgpd:
            return @"sgpd";
            break;
        case SLMP4BoxType_sbgp:
            return @"sbgp";
            break;
        // udta sub
        case SLMP4BoxType_meta:
            return @"meta";
            break;
        // edts sub
        case SLMP4BoxType_elst:
            return @"elst";
            break;
    }
}

- (void)setTypeDesc:(NSString *)typeDesc
{
    if ([typeDesc isEqualToString:@"none"])
    {
        self.type = SLMP4BoxType_none;
    }
    else if ([typeDesc isEqualToString:@"ftyp"])
    {
        self.type = SLMP4BoxType_ftyp;
    }
    else if ([typeDesc isEqualToString:@"free"])
    {
        self.type = SLMP4BoxType_free;
    }
    else if ([typeDesc isEqualToString:@"mdat"])
    {
        self.type = SLMP4BoxType_mdat;
    }
    else if ([typeDesc isEqualToString:@"moov"])
    {
        self.type = SLMP4BoxType_moov;
    }
    // moov or trak sub
    else if ([typeDesc isEqualToString:@"mvhd"])
    {
        self.type = SLMP4BoxType_mvhd;
    }
    else if ([typeDesc isEqualToString:@"trak"])
    {
        self.type = SLMP4BoxType_trak;
    }
    else if ([typeDesc isEqualToString:@"udta"])
    {
        self.type = SLMP4BoxType_udta;
    }
    // trak sub
    else if ([typeDesc isEqualToString:@"tkhd"])
    {
        self.type = SLMP4BoxType_tkhd;
    }
    else if ([typeDesc isEqualToString:@"edts"])
    {
        self.type = SLMP4BoxType_edts;
    }
    else if ([typeDesc isEqualToString:@"mdia"])
    {
        self.type = SLMP4BoxType_mdia;
    }
    // mdia sub
    else if ([typeDesc isEqualToString:@"mdhd"])
    {
        self.type = SLMP4BoxType_mdhd;
    }
    else if ([typeDesc isEqualToString:@"hdlr"])
    {
        self.type = SLMP4BoxType_hdlr;
    }
    else if ([typeDesc isEqualToString:@"minf"])
    {
        self.type = SLMP4BoxType_minf;
    }
    // minf sub
    else if ([typeDesc isEqualToString:@"vmhd"])
    {
        self.type = SLMP4BoxType_vmhd;
    }
    else if ([typeDesc isEqualToString:@"smhd"])
    {
        self.type = SLMP4BoxType_smhd;
    }
    else if ([typeDesc isEqualToString:@"dinf"])
    {
        self.type = SLMP4BoxType_dinf;
    }
    else if ([typeDesc isEqualToString:@"stbl"])
    {
        self.type = SLMP4BoxType_stbl;
    }
    // dinf sub
    else if ([typeDesc isEqualToString:@"dref"])
    {
        self.type = SLMP4BoxType_dref;
    }
    // stbl sub
    else if ([typeDesc isEqualToString:@"stsd"])
    {
        self.type = SLMP4BoxType_stsd;
    }
    else if ([typeDesc isEqualToString:@"stts"])
    {
        self.type = SLMP4BoxType_stts;
    }
    else if ([typeDesc isEqualToString:@"stss"])
    {
        self.type = SLMP4BoxType_stss;
    }
    else if ([typeDesc isEqualToString:@"ctts"])
    {
        self.type = SLMP4BoxType_ctts;
    }
    else if ([typeDesc isEqualToString:@"stsc"])
    {
        self.type = SLMP4BoxType_stsc;
    }
    else if ([typeDesc isEqualToString:@"stsz"])
    {
        self.type = SLMP4BoxType_stsz;
    }
    else if ([typeDesc isEqualToString:@"stco"])
    {
        self.type = SLMP4BoxType_stco;
    }
    else if ([typeDesc isEqualToString:@"sgpd"])
    {
        self.type = SLMP4BoxType_sgpd;
    }
    else if ([typeDesc isEqualToString:@"sbgp"])
    {
        self.type = SLMP4BoxType_sbgp;
    }
    // udta sub
    else if ([typeDesc isEqualToString:@"meta"])
    {
        self.type = SLMP4BoxType_meta;
    }
    // edts sub
    else if ([typeDesc isEqualToString:@"elst"])
    {
        self.type = SLMP4BoxType_elst;
    }
}

@end
